/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.company.workflow.loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.opensymphony.workflow.InvalidWorkflowDescriptorException;
import com.opensymphony.workflow.loader.ValidationHelper;
import com.opensymphony.workflow.util.Validatable;


/**
 * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
 * @version $Revision: 1.13 $
 */
public class ActionDescriptor extends AbstractDescriptor implements Validatable {
    //~ Instance fields ////////////////////////////////////////////////////////

    protected List conditionalResults = new ArrayList();
    protected List postFunctions = new ArrayList();
    protected List preFunctions = new ArrayList();
    protected List validators = new ArrayList();
    protected Map metaAttributes = new HashMap();
    protected RestrictionDescriptor restriction;
    protected UnConditionResultDescriptor unconditionalResult;
    protected String name;
    protected String view;
    protected boolean autoExecute = false;
    protected boolean finish = false;

    //~ Constructors ///////////////////////////////////////////////////////////

    public ActionDescriptor() {
    }


    //~ Methods ////////////////////////////////////////////////////////////////

    public void setAutoExecute(boolean autoExecute) {
        this.autoExecute = autoExecute;
    }

    public boolean getAutoExecute() {
        return autoExecute;
    }

    public List getConditionalResults() {
        return conditionalResults;
    }

    public void setFinish(boolean finish) {
        this.finish = finish;
    }

    public boolean isFinish() {
        return finish;
    }

    public void setMetaAttributes(Map metaAttributes) {
        this.metaAttributes = metaAttributes;
    }

    public Map getMetaAttributes() {
        return metaAttributes;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public List getPostFunctions() {
        return postFunctions;
    }

    public List getPreFunctions() {
        return preFunctions;
    }

    public void setRestriction(RestrictionDescriptor restriction) {
        this.restriction = restriction;
    }

    public RestrictionDescriptor getRestriction() {
        return restriction;
    }

    public void setUnconditionalResult(UnConditionResultDescriptor unconditionalResult) {
        this.unconditionalResult = unconditionalResult;
    }

    public UnConditionResultDescriptor getUnconditionalResult() {
        return unconditionalResult;
    }

    public List getValidators() {
        return validators;
    }

    public void setView(String view) {
        this.view = view;
    }

    public String getView() {
        return view;
    }

    public void validate() throws InvalidWorkflowDescriptorException {
        ValidationHelper.validate(preFunctions);
        ValidationHelper.validate(postFunctions);
        ValidationHelper.validate(validators);
        ValidationHelper.validate(conditionalResults);

        if (restriction != null) {
            restriction.validate();
        }

        if (unconditionalResult != null) {
            unconditionalResult.validate();
        }
    }

 
 

	public String getXML() {

	        StringBuffer buf = new StringBuffer("<action id=\"");
	        buf.append(getId());
	        buf.append("\"");

	        if ((name != null) && (name.length() > 0)) {
	            buf.append(" name=\"");
	            buf.append(name);
	            buf.append("\"");
	        }

	        if ((view != null) && (view.length() > 0)) {
	            buf.append(" view=\"");
	            buf.append(view);
	            buf.append("\"");
	        }

	        if (finish) {
	            buf.append(" finish=\"true\"");
	        }

	        if (autoExecute) {
	            buf.append(" auto=\"true\"");
	        }

	        buf.append(">");

	        Iterator iter = metaAttributes.entrySet().iterator();

	        while (iter.hasNext()) {
	            Map.Entry entry = (Map.Entry) iter.next();
	            buf.append("<meta name=\"");
	            buf.append(entry.getKey());
	            buf.append("\">");
	            buf.append(entry.getValue());
	            buf.append("</meta>");
	        }

	        if (restriction != null) {
	            buf.append(restriction.getXML());
	        }

	        if (validators.size() > 0) {
	            buf.append("<validators>");

	            for (int i = 0; i < validators.size(); i++) {
	                ValidatorDescriptor validator = (ValidatorDescriptor) validators.get(i);
	                buf.append(validator.getXML());
	            }

	            buf.append("</validators>");
	        }

	        if (preFunctions.size() > 0) {
	        	buf.append("<pre-functions>");

	            for (int i = 0; i < preFunctions.size(); i++) {
	                FunctionDescriptor function = (FunctionDescriptor) preFunctions.get(i);
	                buf.append(function.getXML());
	            }

	            
	            buf.append("</pre-functions>");
	        }

	        buf.append("<results>");

	        for (int i = 0; i < conditionalResults.size(); i++) {
	            ResultDescriptor result = (ResultDescriptor) conditionalResults.get(i);
	            buf.append(result.getXML());
	        }

	        if (unconditionalResult != null) {
	            buf.append(unconditionalResult.getXML());
	        }

	        buf.append("</results>");

	        if (postFunctions.size() > 0) {
	            buf.append("<post-functions>");

	            for (int i = 0; i < postFunctions.size(); i++) {
	                FunctionDescriptor function = (FunctionDescriptor) postFunctions.get(i);
	                buf.append(function.getXML());
	            }

	            buf.append("</post-functions>");
	        }

	        buf.append("</action>");
	        
	        return buf.toString();
	}


	public void setConditionalResults(List conditionalResults) {
		this.conditionalResults = conditionalResults;
	}


	public void setPostFunctions(List postFunctions) {
		this.postFunctions = postFunctions;
	}


	public void setPreFunctions(List preFunctions) {
		this.preFunctions = preFunctions;
	}


	public void setValidators(List validators) {
		this.validators = validators;
	}
}
